//
//  main.m
//  04-dictionary
//
//  Created by dllo on 16/7/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Color.h"

int main(int argc, const char * argv[]) {
//    处理⽂件crayons.txt中的⽂本信息，⽂本内容是关于颜⾊的，每⾏都是⼀ 个颜⾊的信息，例如：Almond #EED9C4，前⼀个字符串是颜⾊的名称，后⼀个字符串是颜⾊的16进制⾊值，处理⽂本完成如下需求:
//    
//    使⽤字典管理所有的颜⾊，即字典中存储的是多个键值对，颜⾊名称为 key，16进制颜⾊值（不带#）是value。
//    使⽤⼀个新的字典管理颜⾊，对颜⾊进⾏分类管理，即：“A”，“B”，“C”… 即这个字典包含多个键值对，key是26个字⺟，value是数组，数组⾥存放的是 Color对象（包含name和colorValue）。需要⾃⼰创建Color类。
        NSString *dataString = [NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/04-dictionary/04-dictionary/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@", dataString);
    NSArray *arr = [dataString componentsSeparatedByString:@"\n"];
//    [arr removeObjectAtIndex:(arr.count - 1)];
    NSLog(@"%@", arr);
    NSMutableArray *arr1 = [NSMutableArray array];
    for (int i = 0; i < [arr count]; i++) {
        NSArray *arr2 = [arr[i] componentsSeparatedByString:@" #"];
        [arr1 addObjectsFromArray:arr2];
    }
    NSLog(@"_-----------");
    NSLog(@"%@", arr1);
  
    [arr1 removeObjectAtIndex:(arr1.count - 1)];
    // 定义可变数组储存key
    NSMutableArray *arrayOfKey = [NSMutableArray array];
    // 定义可变数组储存value.
     NSMutableArray *arrayOfValue = [NSMutableArray array];
    for (int i = 0; i < arr.count; i++) {
        if ((i + 2) % 2 == 0) {
            [arrayOfKey addObject:arr1[i]];
        } else {
            [arrayOfValue addObject:arr1[i]];
        }
    }
    
    NSLog(@"%@", arrayOfKey);
    NSLog(@"%@", arrayOfValue);
    NSLog(@"________%ld______%ld", arrayOfKey.count, arrayOfValue.count);
    // 创建字典,key为颜色,value为二进制数字
    NSMutableDictionary *colorDic = [NSMutableDictionary dictionaryWithObjects:arrayOfKey forKeys:arrayOfValue];
    NSLog(@"%@", colorDic);
    //取出字典中所有的key值
    NSArray *arrKey = [colorDic allKeys];
    // 对字典中的key排序*用方法选择排序方法
    NSArray *arr4 = [arrKey sortedArrayUsingSelector:@selector(compare:)];
    // 打印排序后的数组
    NSLog(@"%@", arr4);
    
    NSMutableArray *arr5 = [NSMutableArray array];
    //定义数组用来春城value
    for (NSString *key in arr4) {
        NSString *value = [colorDic objectForKey:key];
        [arr5 addObject:value];
    }
    NSLog(@"%@", arr5);
    
    // 2问题
     NSMutableDictionary *dicColorClass = [NSMutableDictionary dictionary];
    
    
    
    for (NSString *str in arr) {
        NSArray *arr = [str componentsSeparatedByString:@"#"];
        Color *color = [[Color alloc] initWithName:[arr firstObject] coded:[arr lastObject]];
        NSString *name = [arr firstObject];
        if (name.length > 0) {
            NSString *first = [[name substringWithRange:NSMakeRange(0, 1)] uppercaseString];
            NSMutableArray *marr = [dicColorClass objectForKey:first];
            if (marr == nil) {
                NSMutableArray *marrOf = [NSMutableArray arrayWithObject:color];
                [dicColorClass setObject:marrOf forKey:first];
            } else {
                [marr addObject:color];
            }
        }
    }
    NSLog(@"%@", dicColorClass);
    

    
    
    return 0;
}
